{extend name="public/base"}

{block name="css"}
{/block}

{block name="main"}
<div class="layui-card">
    <blockquote class="layui-elem-quote layui-quote-nm">{empty name="data"}添加{else /}编辑{/empty}角色</blockquote>
    <form class="layui-form" action="" lay-filter="component-form-group">
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" placeholder="请输入"
                       autocomplete="off"
                       class="layui-input" value="{$data.name|default=''}">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="checkbox" {eq name="data.status|default='99'" value="99" } checked="" {/eq} lay-skin="switch"
                lay-filter="component-form-switchTest" lay-text="开启|关闭">
            </div>
            <input name="status" type="hidden" value="{$data.status|default='99'}"/>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <!--<div class="layui-footer">-->
                <input type="hidden" name="id" value="{$data.id|default=0}">
                <button class="layui-btn" lay-submit="" lay-filter="component-form-demo1">提 交</button>
                <!--</div>-->
            </div>
        </div>
    </form>
</div>
{/block}

{block name="js"}
<script>
    layui.config({
        base: '/layui_admin/src/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function () {
        let $ = layui.$
            , form = layui.form, admin = layui.admin;

        /* 监听指定开关 */
        form.on('switch(component-form-switchTest)', function (data) {
            let input_status = $('input[name="status"]');
            console.log(this.checked);
            this.checked ? input_status.val(99) : input_status.val(10);
        });

        /* 监听提交 */
        form.on('submit(component-form-demo1)', function (data) {
            //请求登入接口
            admin.req({
                url: "{:url('save_role')}" //实际使用请改成服务端真实接口
                ,type : 'post'
                ,data: data.field
                ,done: function(res){
                    //登入成功的提示与跳转
                    layer.msg('保存成功！', {
                        offset: '15px'
                        ,icon: 1
                        ,time: 1000
                    }, function(){
                        parent.location.reload(); //刷新页面
                    });
                }
            });
            return false;
        });

        /**
         * 列表加载完成之后的回调函数
         * @param d
         */
        layui.data.done = function(d){
            console.log(d);
            layui.use(['form'], function(){
                form.render(null, 'component-form-group');
            });
        };
    });
</script>
{/block}